package vip.xiaonuo.supplier.modular.finance.settlementproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.finance.settlementproject.entity.SupplierSettlementProject;
import vip.xiaonuo.supplier.modular.finance.settlementproject.mapper.SupplierSettlementProjectMapper;
import vip.xiaonuo.supplier.modular.finance.settlementproject.param.SupplierSettlementProjectAddParam;
import vip.xiaonuo.supplier.modular.finance.settlementproject.param.SupplierSettlementProjectEditParam;
import vip.xiaonuo.supplier.modular.finance.settlementproject.param.SupplierSettlementProjectIdParam;
import vip.xiaonuo.supplier.modular.finance.settlementproject.param.SupplierSettlementProjectPageParam;
import vip.xiaonuo.supplier.modular.finance.settlementproject.service.SupplierSettlementProjectService;

import java.util.List;

/**
 * 结算项目Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/04/10 09:59
 **/
@Service
public class SupplierSettlementProjectServiceImpl extends ServiceImpl<SupplierSettlementProjectMapper, SupplierSettlementProject> implements SupplierSettlementProjectService {

    @Override
    public Page<SupplierSettlementProject> page(SupplierSettlementProjectPageParam supplierSettlementProjectPageParam) {
        QueryWrapper<SupplierSettlementProject> queryWrapper = new QueryWrapper<SupplierSettlementProject>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierSettlementProjectPageParam.getProjectType())) {
            queryWrapper.lambda().eq(SupplierSettlementProject::getProjectType, supplierSettlementProjectPageParam.getProjectType());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierSettlementProject::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierSettlementProjectPageParam.getSortField(), supplierSettlementProjectPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierSettlementProjectPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierSettlementProjectPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierSettlementProjectPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierSettlementProject::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierSettlementProjectAddParam supplierSettlementProjectAddParam) {
        List<SupplierSettlementProject> supplierSettlementProjects = this.list(new LambdaQueryWrapper<SupplierSettlementProject>().eq(SupplierSettlementProject::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId()).eq(SupplierSettlementProject::getProjectCode, supplierSettlementProjectAddParam.getProjectCode()));
        if(ObjectUtil.isNotEmpty(supplierSettlementProjects)) {
            throw new CommonException("结算项目编码重复，项目名称是：{}", supplierSettlementProjects.get(0).getProjectName());
        }
        SupplierSettlementProject supplierSettlementProject = BeanUtil.toBean(supplierSettlementProjectAddParam, SupplierSettlementProject.class);
        //数据归属
        supplierSettlementProject.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierSettlementProject);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierSettlementProjectEditParam supplierSettlementProjectEditParam) {
        List<SupplierSettlementProject> supplierSettlementProjects = this.list(new LambdaQueryWrapper<SupplierSettlementProject>().eq(SupplierSettlementProject::getCompanyId, supplierSettlementProjectEditParam.getCompanyId()).eq(SupplierSettlementProject::getProjectCode, supplierSettlementProjectEditParam.getProjectCode()).ne(SupplierSettlementProject::getId, supplierSettlementProjectEditParam.getId()));
        if(ObjectUtil.isNotEmpty(supplierSettlementProjects)) {
            throw new CommonException("结算项目编码重复，项目名称是：{}", supplierSettlementProjects.get(0).getProjectName());
        }
        SupplierSettlementProject supplierSettlementProject = this.queryEntity(supplierSettlementProjectEditParam.getId());
        BeanUtil.copyProperties(supplierSettlementProjectEditParam, supplierSettlementProject);
        this.updateById(supplierSettlementProject);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierSettlementProjectIdParam> supplierSettlementProjectIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierSettlementProjectIdParamList, SupplierSettlementProjectIdParam::getId));
    }

    @Override
    public SupplierSettlementProject detail(SupplierSettlementProjectIdParam supplierSettlementProjectIdParam) {
        return this.queryEntity(supplierSettlementProjectIdParam.getId());
    }

    @Override
    public SupplierSettlementProject queryEntity(String id) {
        SupplierSettlementProject supplierSettlementProject = this.getById(id);
        if(ObjectUtil.isEmpty(supplierSettlementProject)) {
            throw new CommonException("结算项目不存在，id值为：{}", id);
        }
        return supplierSettlementProject;
    }
}
